package com.example.endproject;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {
    private static final String TAG = "RegisterActivity";
    String userName,pwd,replyPwd;
    TextView userNameView,pwdView,replyPwdView;
    DBHelper dbHelper;
    SQLiteDatabase mSqlDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper = new DBHelper(this);
        mSqlDB = dbHelper.getWritableDatabase();
        //初始化控件
        TextView subjectView = findViewById(R.id.register2_subject);
        subjectView.setTypeface(Typeface.createFromAsset(getAssets(),"font/FZBangSXJW.ttf"));
        userNameView = findViewById(R.id.register_username);
        pwdView = findViewById(R.id.register_pwd);
        replyPwdView = findViewById(R.id.register_replyPwd);
    }
    public void register1(View view){
        //获取用户输入的数据
        userName = userNameView.getText().toString();
        pwd = pwdView.getText().toString();
        replyPwd = replyPwdView.getText().toString();
        if(userName.equals("")||pwd.equals("")||replyPwd.equals("")){
            Toast toast = Toast.makeText(getApplicationContext(), "不能为空", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            return;
        }
        //首先判断数据库中是否存在相同的用户名
        Cursor query = mSqlDB.query(DBHelper.TB_NAME_USER, new String[]{"user_name", "pwd"}, "user_name = ?",
                new String[]{userName}, null, null, null, null);
        if(query.moveToFirst()){//有相同的
            query.close();
            userNameView.setText("");
            userNameView.setHint("存在相同用户名");
            return;
        }
        query.close();
        //再判断两次密码是否相同
        if (!pwd.equals(replyPwd)){
            replyPwdView.setText("");
            replyPwdView.setHint("两次密码输入不一致");
            replyPwdView.setHintTextColor(0xFF0000);
            return;
        }
        //都成功后进入下一步
        Intent intent = new Intent(this,RegisterActivity2.class);
        intent.putExtra("userName",userName);
        intent.putExtra("pwd",pwd);
        startActivityForResult(intent,1);
    }
}